package com.zito.pojo;

import java.sql.SQLException;
import java.util.Map;

import com.downlove.cache.CacheManager;
import com.downlove.common.Inflector;
import com.downlove.database.DBException;
import com.downlove.database.POJO;
import com.downlove.database.QueryHelper;
import com.downlove.database.SequenceManager;

@SuppressWarnings("serial")
public class AdvicePOJO extends POJO {

	private Object[] valueObj;
	private String __this_table_name;

	public long Save() {
		this.setId(SequenceManager.nextID(this.getClass().getSimpleName()));
		boolean flag = true;
		try {
			QueryHelper.setAutoCommit();
			QueryHelper.commitUpdate(_InsertSQL(this), this.valueObj);
			QueryHelper.commit();
		} catch (Exception e) {
			flag = false;
			try {
				QueryHelper.rollback();
			} catch (SQLException e1) {
				throw new DBException(e1);
			}
			throw new DBException(e);
		} finally {
			try {
				QueryHelper.closeTransConnection();
			} catch (SQLException e2) {
				throw new DBException(e2);
			}
		}
		if (this.IsObjectCachedByID())
			CacheManager.evict(CacheRegion(), OBJ_COUNT_CACHE_KEY);
		return flag ? this.getId() : -1;
	}

	public long Update() {
		boolean flag = true;
		try {
			QueryHelper.setAutoCommit();
			QueryHelper.commitUpdate(_updateSQL("isreport","isshow","ansid","ansname","anstime","anscontent","id"), this.valueObj);
			QueryHelper.commit();
		} catch (Exception e) {
			flag = false;
			try {
				QueryHelper.rollback();
			} catch (SQLException e1) {
				throw new DBException(e1);
			}
			throw new DBException(e);
		} finally {
			try {
				QueryHelper.closeTransConnection();
			} catch (SQLException e2) {
				throw new DBException(e2);
			}
		}
		Evict(getId());
		return flag ? this.getId() : -1;
	}

	private String _updateSQL(String... args) {
		Map<String, Object> pojo_bean = this.ListInsertableFields();
		StringBuilder sql = new StringBuilder("UPDATE ");
		sql.append(TableName());
		sql.append(" SET ");
		valueObj = new Object[args.length];
		for (int i = 0; i < args.length - 1; i++) {
			if (i > 0)
				sql.append(',');
			sql.append(args[i] + "=?");
			valueObj[i] = pojo_bean.get(args[i]);
		}
		valueObj[args.length - 1] = pojo_bean.get(args[args.length - 1]);
		sql.append(" WHERE " + args[args.length - 1] + "=?");
		return sql.toString();
	}

	private String _InsertSQL(AdvicePOJO obj) {
		Map<String, Object> pojo_bean = obj.ListInsertableFields();
		String[] fields = pojo_bean.keySet().toArray(
				new String[pojo_bean.size()]);
		StringBuilder sql = new StringBuilder("INSERT INTO ");
		sql.append(obj.TableName());
		sql.append('(');
		valueObj = new Object[fields.length];
		for (int i = 0; i < fields.length; i++) {
			if (i > 0)
				sql.append(',');
			sql.append(fields[i]);
		}
		sql.append(") VALUES(");
		for (int i = 0; i < fields.length; i++) {
			if (i > 0)
				sql.append(',');
			sql.append('?');
			valueObj[i] = pojo_bean.get(fields[i]);
		}
		sql.append(')');
		return sql.toString();
	}

	protected String TableName() {
		if (__this_table_name == null)
			__this_table_name = Inflector.getInstance().tableize(getClass());
		return __this_table_name;
	}
}
